﻿IF EXISTS (
       SELECT *
       FROM   sys.objects
       WHERE  OBJECT_ID = OBJECT_ID(N'[dbo].[ws_insertNewDocument]')
              AND TYPE IN (N'P', N'PC')
   )
    DROP PROCEDURE [dbo].[ws_insertNewDocument]
GO

CREATE PROCEDURE ws_insertNewDocument
	@DocName NVARCHAR(100) ,
	@DocLength FLOAT ,
	@ObjectType VARCHAR(20) ,
	@ObjectID VARCHAR(40)
AS
BEGIN
	DECLARE @DocID INT
	SET @DocID = 0
	IF EXISTS (
	       SELECT TOP 1 wd.DocID
	       FROM   ws_Document wd
	   )
	BEGIN
	    SET @DocID = (
	            SELECT MAX(wd.DocID)
	            FROM   ws_Document wd
	        )
	    
	    SET @DocID = @DocID + 1
	END
	
	INSERT INTO ws_Document
	  (
	    DocID,
	    DocName,
	    DocLength,
	    ObjectType,
	    ObjectID
	  )
	VALUES
	  (
	    @DocID/* DocID	*/,
	    @DocName/* DocName	*/,
	    @DocLength /* DocLength	*/,
	    @ObjectType/* ObjectType	*/,
	    @ObjectID/* ObjectID	*/
	  )
END